//
//  DYGuidePageView.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/10.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYGuidePageView.h"

@interface DYGuidePageView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) UIImageView *imageView;


@end


@implementation DYGuidePageView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    CGFloat height;
    if (XHQ_iPhoneX) {
        height=BILIHEIGHT(180);
    }else{
        height=BILIHEIGHT(80);
    }
    self.scrollView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self addSubview:self.scrollView];
    
    for (int i = 0; i < self.arr.count; i++) {
        self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.arr[i]]];
        self.imageView.frame = CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight);
        self.imageView.userInteractionEnabled = YES;
        [self.scrollView addSubview:self.imageView];
        
        CGFloat height;
        if (XHQ_iPhoneX) {
            height=BILIHEIGHT(120);
        }else{
            height=BILIHEIGHT(100);
        }
        UIButton *button=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:[UIColor clearColor] action:@selector(nextButtonClick:) target:self title:nil image:nil font:12 textColor:WhiteColor];
        button.frame=CGRectMake(kScreenWidth * i+(kScreenWidth-BILIWIDTH(80)), kScreenHeight-height, BILIWIDTH(80), BILIHEIGHT(90));
        button.tag=10000+i;
        [self.scrollView addSubview:button];
        
        if (i==self.arr.count-1) {
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Click)];
            [self.imageView addGestureRecognizer:tap];
        }
        
    }
    
    
}

-(void)nextButtonClick:(UIButton *)button
{
    if (button.tag==10000+2) {
         self.pushBlock(1);
    }else{
        self.scrollView.contentOffset=CGPointMake(kScreenWidth*(button.tag-9999), 0);
    }
}

- (NSMutableArray *)arr{
    if (_arr == nil) {
        if (XHQ_iPhoneX) {
            _arr = [[NSMutableArray alloc]initWithObjects:@"guide04",@"guide05",@"guide06",nil];
        }else{
            _arr = [[NSMutableArray alloc]initWithObjects:@"guide01",@"guide02",@"guide03",nil];
        }
    }
    return _arr;
}

- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _scrollView.pagingEnabled=YES;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.delegate=self;
        _scrollView.contentSize=CGSizeMake( kScreenWidth * self.arr.count, kScreenHeight);
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

//移动scrollerView让小圆点偏移
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //    self.pageControl.currentPage = (NSInteger) (scrollView.contentOffset.x / kScreenWidth);
    if (scrollView.contentOffset.x>kScreenWidth*2+BILIWIDTH(30)) {
        self.pushBlock(1);
    }
}
- (void)Click{
    self.pushBlock(1);
    
}



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
